//
//  ShoppingCarCell.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/23.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "ShoppingCarCell.h"
#define NUM_PAD_DONE_BUTTON_TAG 8888
@interface ShoppingCarCell ()<UITextFieldDelegate>
{
    __weak IBOutlet UIImageView *img;
    __weak IBOutlet UILabel *nameLab;
    __weak IBOutlet UILabel *needLab;
    __weak IBOutlet UILabel *leftLab;
    __weak IBOutlet UITextField *countLab;
    __weak IBOutlet UIButton *bwBtn;
    
    //记录当前model
    WYShopCarModel *_currentModel;
}
@end
@implementation ShoppingCarCell

- (void)awakeFromNib {
    // Initialization code
    [self addSubview:[WYPublic getFenGexian:0 :self.frame.size.height - 1 :WYMWITH :0.5]];
    
    AFFNumericKeyboard *keyboard = [[AFFNumericKeyboard alloc] initWithFrame:CGRectMake(0, WYMHEIGHT-216, WYMWITH, 216)];
    countLab.inputView = keyboard;
    keyboard.delegate = self;
    
//    bwBtn.layer.cornerRadius = 2;
//    bwBtn.layer.masksToBounds = YES;
//    bwBtn.layer.borderWidth = 1;
    bwBtn.selected = NO;
//    bwBtn.layer.borderColor = QBB_RED_COLOR.CGColor;
    [bwBtn setTitleColor:QBB_RED_COLOR forState:UIControlStateNormal];
    [bwBtn setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
    
    

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}
//填充cell的方法
- (void)fillCellWithModel:(WYShopCarModel *)model
{
    countLab.delegate = self;
    _currentModel = model;
    [img sd_setImageWithURL:[NSURL URLWithString:[WYPublic getUsefullStr:model.imgPath]] placeholderImage:[UIImage imageNamed:@"moren"]];
    if (model.qishu == nil) {
        model.qishu = @"";
    }
    nameLab.text = [NSString stringWithFormat:@"(第%@期)%@",model.qishu,model.name];
    if (model.all_need != nil) {
        needLab.text = [NSString stringWithFormat:@"总需:%@次",model.all_need];
    }
    if (model.left_need != nil) {
        leftLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"剩余:" Middle:model.left_need RightStr:@"次" Font:14];
    }
    
    if (model.left_need.intValue < 5){
        
        countLab.text = model.left_need;
    }else{
        countLab.text = [NSString stringWithFormat:@"%d",model.count];
    }
    
    
    
    //包尾处理
    if ([countLab.text intValue] >= [_currentModel.left_need intValue]) {
        bwBtn.selected = YES;
        bwBtn.layer.borderColor = [UIColor grayColor].CGColor;
    }else{
        bwBtn.selected = NO;
        bwBtn.layer.borderColor = QBB_RED_COLOR.CGColor;
    }
}
//加按钮的点击事件
- (IBAction)addBtnOnClick:(id)sender {
    
    if (((countLab.text.integerValue + 1) > CHONGZHI_LIMIT)){
//        [WYPublic showErrorWithTitle:@"" Msg:@"亲 每次最多不能超过1000哦!"];
        return;
    }
    
    if (([countLab.text intValue]+1)<=[_currentModel.left_need intValue])  {
        if ([[WYDataBase sharedDataBase]updataCountInCar:[countLab.text intValue]+1 pdc:[_currentModel.car_new_id intValue]]) {
            countLab.text = [NSString stringWithFormat:@"%d",[countLab.text intValue]+1];
            _currentModel.count = [countLab.text intValue];
        }
    }
    //刷新计数
    [self.father reloadFooter];
}
//减按钮的点击事件
- (IBAction)subBtnOnClick:(id)sender {
    if ([countLab.text intValue]-1 > 0) {
        if ([[WYDataBase sharedDataBase]updataCountInCar:[countLab.text intValue]-1 pdc:[_currentModel.car_new_id intValue]]) {
            countLab.text = [NSString stringWithFormat:@"%d",[countLab.text intValue]-1];
            _currentModel.count = [countLab.text intValue];
        }
    }
    //刷新计数
    [self.father reloadFooter];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.father.currentCellIndex = self.currentCellIndex;
    //调整偏移值
    [self.father changeOffsetWithFrame:self.frame];
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self doneBtnOnClick];
}

#pragma mark -- 自定义键盘代理
-(void)changeKeyboardType
{
    [countLab resignFirstResponder];
    countLab.inputView = nil;
    [countLab becomeFirstResponder];
}

-(void)numberKeyboardBackspace
{
    if (countLab.text.length != 0)
    {
        countLab.text = [countLab.text substringToIndex:countLab.text.length -1];
    }
}

-(void)numberKeyboardInput:(NSInteger)number
{
    countLab.text = [countLab.text stringByAppendingString:[NSString stringWithFormat:@"%ld",(unsigned long)number]];
}



//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    [countLab resignFirstResponder];
//}
//完成按钮
- (void) doneBtnOnClick
{
    if (((countLab.text.integerValue + 1) > CHONGZHI_LIMIT)){
//        [WYPublic showErrorWithTitle:@"" Msg:@"亲 每次最多不能超过1000哦!"];
        countLab.text = [NSString stringWithFormat:@"%d",CHONGZHI_LIMIT];
        
    }
    if (countLab.text.length != 0 && [countLab.text intValue]>0) {
        //不能大于剩余数
        if ([countLab.text intValue] > [_currentModel.left_need intValue]) {
            countLab.text = _currentModel.left_need;
            if ([_currentModel.left_need isEqual:@"0"]) {
                countLab.text = @"0";
                return;
            }
        }
        
    }else{
        if ([_currentModel.left_need isEqual:@"0"]) {
            countLab.text = @"0";
        }else{
            countLab.text = @"1";
        }
        
    }
    
    _currentModel.count = [countLab.text intValue];
    //改变库中计数
    if ([[WYDataBase sharedDataBase]updataCountInCar:_currentModel.count pdc:[_currentModel.car_new_id  intValue]] && countLab.text.length!=0 && [countLab.text integerValue]!=0) {
        //刷新计数
        [self.father reloadFooter];
    }
    //关闭键盘
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    [self.father backOffset];
    
}
//点击空白处关闭键盘
- (void)closeKeyBoardByVC
{
    [self doneBtnOnClick];
}

//包尾
//- (IBAction)baowei:(UIButton *)sender {
//    
//    if(sender.selected){
//        bwBtn.layer.borderColor = QBB_RED_COLOR.CGColor;
//        if (_currentModel.beforeCount == 0) {
//            if ([_currentModel.left_need intValue]<5) {
//                _currentModel.beforeCount = [_currentModel.left_need intValue];
//            }else{
//                _currentModel.beforeCount = 5;
//            }
//        }
//        //还原数据
//        countLab.text = [NSString stringWithFormat:@"%d",_currentModel.beforeCount];
//    }else{
//        bwBtn.layer.borderColor = [UIColor grayColor].CGColor;
//        if (_currentModel.left_need != nil && [_currentModel.left_need intValue] > 0) {
//            //记录包尾前的数据
//            _currentModel.beforeCount = [countLab.text intValue];
//            //包尾
//            countLab.text = _currentModel.left_need;
//        }
//    }
//    if (//刷新数据库数据
//        [[WYDataBase sharedDataBase]updataCountInCar:[countLab.text intValue] pdc:[_currentModel.car_id  intValue]]) {
//        sender.selected = !sender.selected;
//        //刷新计数
//        _currentModel.count = [countLab.text intValue];
//        [self.father reloadFooter];
//    }
//    
//}



@end
